<style scoped>
.form-list-data-table {
  margin: 12px 0;
}
.form-list-data-table /deep/ .el-table th {
  color: #555;
}
</style>
<!--  -->
<template>
  <div class="form-list-data-table">
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="表单名称">
      </el-table-column>
      <el-table-column prop="total" label="提交数据总数" align="center">
      </el-table-column>
      <el-table-column label="挂载系统" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.mount && scope.row.mount.length > 0">
            <span v-for="(item, index) in scope.row.mount" :key="item.id">
              <span v-if="index===0">{{item.name}}</span>
              <span v-else>、{{item.name}}</span>
            </span>
          </span>
          <span v-else style="color:#999999;">未挂载</span>
        </template>
      </el-table-column>
      <el-table-column prop="createAt" label="创建时间" width="180">
        <template slot-scope="scope">
          {{scope.row.createAt | dateformat('YYYY-MM-DD')}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="380">
        <template slot-scope="scope">
          <el-button @click="goFormInfo(scope.row.id)" type="text " size="small">进入</el-button>
          <!-- <el-button @click="editorForm(scope.row.id)" type="text" size="small">编辑</el-button> -->
          <el-button @click="setForm(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteForm(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑表单 -->
    <el-dialog
      title="编辑表单"
      :visible.sync="dialogFormVisible"
      width="30%"
      @closed="closed"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        v-loading="labelLoading"
        :rules="rules"
        ref="labelSave"
        label-width="80px"
        @submit.native.prevent
      >
        <el-form-item
          label="表单名称"
          prop="formName"
        >
          <el-input
            v-model.trim="form.formName"
            autocomplete="off"
            size="medium"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item
          label="挂载系统"
          prop="systemId"
        >
          <el-select size="medium" v-model="form.systemId" style="width: 355px;" placeholder="选择挂载系统">
            <el-option v-for="item in systemMount" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button size="small" @click="closed">取 消</el-button>
        <el-button size="small" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <DialogFormEdit :resetData="resetData" ref="dialogFormEdit" />
  </div>
</template>

<script>
import DialogFormEdit from '@/components/main/user/form/sysformlist/DialogFormEdit.vue'
import { getSysFormListPathByItem } from '@/core/functions.js'
export default {
  props: {
    dataTotal: {
      type: Function
    }
  },
  data () {
    var checkIshas = (rule, value, callback) => {
      let bool = true
      this.tableData.map(item => {
        if (item.id !== this.form.id) {
          if (value === item.name) {
            bool = false
          }
        }
      })
      if (!bool) {
        callback(new Error('表单名称已存在'))
      } else {
        return callback()
      }
    }
    return {
      loading: true,
      // 表格数据
      tableData: [],
      // 请求返回的数据
      data: [],
      // 可挂载系统
      // systemMount: [],
      dialogFormVisible: false,
      labelLoading: false,
      form: {},
      rules: {
        formName: [
          { required: true, message: '请输入表单名称', trigger: 'blur' },
          { min: 1, max: 24, message: '表单名称长度不能超过24个字！', trigger: 'blur' },
          { validator: checkIshas, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 获取可挂载表单的系统数据
    // this.$post('/system/allow-mount-form-system-list').then(response => {
    //   if (response && response.length > 0) {
    //     this.systemMount = response
    //   }
    // })
    this.initData()
  },
  methods: {
    initData () {
      this.loading = true
      this.$post('/form/system-form-list').then((response) => {
        this.loading = false
        if (response && response.length > 0) {
          this.data = response
          this.tableData = makeInitData(response)
        } else {
          this.data = []
          this.tableData = []
        }
      })
    },
    getTableData () {
      return this.tableData
    },
    goFormInfo (formId) {
      let item = this.data.find(element => {
        return parseInt(element.id) === parseInt(formId)
      })
      let fullPath = getSysFormListPathByItem(item)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    editorForm (formId) {
      let item = this.data.find(element => {
        return parseInt(element.id) === parseInt(formId)
      })
      if (!item) {
        return
      }
      this.form = JSON.parse(JSON.stringify(item))
      this.dialogFormVisible = true
    },
    setForm (formId) {
      this.$refs['dialogFormEdit'].show(formId)
    },
    submit () {
      this.$refs['labelSave'].validate((valid) => {
        if (valid) {
          this.labelLoading = true
          this.$post('/form/system-form-update', {
            id: this.form.id,
            name: this.form.formName
          }).then(response => {
            this.initData()
            this.labelLoading = false
            this.dialogFormVisible = false
          })
        } else {
          return false
        }
      })
    },
    closed () {
      this.labelLoading = false
      this.form = {}
      if (this.$refs['labelSave']) {
        this.$refs['labelSave'].clearValidate()
      }
      this.dialogFormVisible = false
    },
    deleteForm (formId) {
      let item = this.data.find(element => {
        return parseInt(element.id) === parseInt(formId)
      })
      if (item.quote > 0) {
        this.$alert('先删除系统中表单的引用再删除表单！')
        return
      }
      // 删除
      this.$confirm('删除后前台提交的表单的数据均会清除，确定?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$post('/form/system-form-delete', {
          formId: formId
        }).then(response => {
          this.loading = false
          this.initData()
        })
      }).catch(() => { })
    },
    resetData () {
      this.initData()
    }
  },
  components: {
    DialogFormEdit
  }
}

function makeInitData (data) {
  return data.map(element => {
    return {
      id: element.id,
      name: element.formName,
      total: element.total,
      quote: element.quote,
      createAt: element.createAt,
      mount: element.mount
    }
  })
}

</script>
